package com.ygqh.baby.utils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ygqh.baby.po.YgAttribute;
import com.ygqh.baby.service.YgAttributeService;

@Component
public class TuanAttributeComparator  implements Comparator<String> {

	private final Long TUAN_ATTRIBUTE_PARENT_ID = 16L;

	@Autowired
	private YgAttributeService ygAttributeService;
	
	private List<String> attributeList = new ArrayList<>();
	
	public void init() {
		attributeList.clear();
		List<YgAttribute> list = ygAttributeService.findByParentId(TUAN_ATTRIBUTE_PARENT_ID);
		list.sort(new Comparator<YgAttribute>() {
			@Override
			public int compare(YgAttribute o1, YgAttribute o2) {
				return o1.getSortNum().compareTo(o2.getSortNum());
			}
		});
		for (YgAttribute ygAttribute : list) {
			String attributeName = ygAttribute.getAttributeName();
			attributeList.add(attributeName.replaceAll("拼团-", ""));
		}
	}

	@Override
	public int compare(String o1, String o2) {
		init();
		return attributeList.indexOf(o1) - attributeList.indexOf(o2);
	}
	
}
